package com.fand.configuration.service;

import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.util.CollectionUtils;

import com.fand.configuration.dao.DictionaryDao;
import com.fand.configuration.dto.DictionaryDeDto;
import com.fand.configuration.dto.DictionaryDto;
import com.fand.configuration.dto.DictionaryTyDto;
import com.fand.dto.PaginationDto;

/**
 * 字典服务
 * 
 * @author yaoHT
 */
public class DefaultDictionaryService implements DictionaryService {
	private ConfigurationService configurationService;
	
	private DictionaryDao dictionaryDao;
	
	@Override
	public int deleteDictionaryDeBy1(final String entityId) {
		return this.dictionaryDao.deleteDictionaryDeBy1(entityId);
	}
	
	@Override
	public int deleteDictionaryTyByIds(final String ids) {
		return this.dictionaryDao.deleteDictionaryTyByIds(ids);
	}
	
	@Override
	public PaginationDto<Map<String, Object>> paginationDictionaryTyBy1ForMapList(
	        final DictionaryTyDto dictionaryTy) {
		return this.dictionaryDao.paginationDictionaryTyBy1ForMapList(dictionaryTy);
	}
	
	@Override
	@CacheEvict(value = DictionaryService.CACHE_NAME, allEntries = true)
	public void reload() {
	}
	
	@Override
	public String saveDictionaryBy1ForObject(final DictionaryDto dictionary) {
		return this.dictionaryDao.saveDictionaryBy1ForObject(dictionary);
	}
	
	@Override
	public int saveDictionaryDeBy1(final DictionaryDeDto dictionaryDe) {
		return this.dictionaryDao.saveDictionaryDeBy1ForObject(dictionaryDe);
	}
	
	@Override
	public String saveDictionaryTyBy1ForObject(final DictionaryTyDto dictionaryTy) {
		return this.dictionaryDao.saveDictionaryTyBy1ForObject(dictionaryTy);
	}
	
	@Override
	public String selectDictionaryBy1ForObject(final String dictionaryTyCo, final String dictionaryCo) {
		return this.dictionaryDao.selectDictionaryBy1ForObject(dictionaryTyCo, dictionaryCo);
	}
	
	@Override
	public int selectDictionaryTyBy201ForObject(final DictionaryTyDto dictionaryTy) {
		return this.dictionaryDao.selectDictionaryTyBy201ForObject(dictionaryTy);
	}
	
	public void setConfigurationService(final ConfigurationService configurationService) {
		this.configurationService = configurationService;
	}
	
	public void setDictionaryDao(final DictionaryDao dictionaryDao) {
		this.dictionaryDao = dictionaryDao;
	}
	
	@Override
	public List<DictionaryTyDto> treeDictionaryTyBy1ForMappedObject(final DictionaryTyDto dictionaryTy) {
		final List<DictionaryTyDto> dictionaryTyList = this.dictionaryDao
		        .selectDictionaryTyBy201ForMappedObject(dictionaryTy);
		
		if (CollectionUtils.isEmpty(dictionaryTyList)) { return dictionaryTyList; }
		
		for (final DictionaryTyDto dictionaryTyDto : dictionaryTyList) {
			if (StringUtils.isBlank(dictionaryTyDto.getId())) {
				continue;
			}
			
			final DictionaryTyDto selectDictionaryTy = new DictionaryTyDto();
			
			org.springframework.beans.BeanUtils.copyProperties(dictionaryTy, selectDictionaryTy);
			
			selectDictionaryTy.setParentId(dictionaryTyDto.getId());
			
			final List<DictionaryTyDto> childs = this.treeDictionaryTyBy1ForMappedObject(selectDictionaryTy);
			
			dictionaryTyDto.setChilds(childs);
		}
		
		return dictionaryTyList;
	}
}